//
//  BBussinessLicenseVC.m
//  DistributorAB
//
//  Created by LZY on 2019/9/27.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BBussinessLicenseVC.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "BBussinessLicenseUploadView.h"

@interface BBussinessLicenseVC () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,BBussinessLicenseUploadViewDelegate>
@property (nonatomic, strong) UIButton *btnImage;//营业执照按钮
@property (nonatomic, strong) UIImage *ivImage;//图片
@property (nonatomic, strong) UIButton *btnDelete;//删除按钮
@end

@implementation BBussinessLicenseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"上传营业执照";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xf2f2f2);
    
    [self setupViewAction];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    
    UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, 176)];
    viewTop.backgroundColor = kColorWithHex(0xffffff);
    [self.view addSubview:viewTop];
    
    UIImageView *ivNotice = [[UIImageView alloc] initWithFrame:CGRectMake(16, 19, 19, 15)];
    ivNotice.image = [UIImage imageNamed:@"user_bussiness_license_icon"];
    [viewTop addSubview:ivNotice];
    
    UILabel *lblNotice = [[UILabel alloc] initWithFrame:CGRectMake(47, 16.5, kScreen_Width - 94, 20)];
    lblNotice.text = @"营业执照";
    lblNotice.textColor = kColorWithHex(0x333333);
    lblNotice.font = [UIFont boldSystemFontOfSize:14];
    [viewTop addSubview:lblNotice];
    
    [viewTop addSubview:self.btnImage];
    
    [viewTop addSubview:self.btnDelete];
    [self.btnDelete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.btnImage.mas_trailing).offset(-16);
        make.bottom.equalTo(self.btnImage.mas_top).offset(16);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    
    UILabel *lblAlertRed = [UILabel new];
    [self.view addSubview:lblAlertRed];
    lblAlertRed.text = @"*";
    lblAlertRed.textColor = kColorWithHex(0xff000a);
    lblAlertRed.font = [UIFont systemFontOfSize:16];
    [lblAlertRed mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(16);
        make.top.equalTo(viewTop.mas_bottom).offset(6);
        make.size.mas_equalTo(CGSizeMake(8, 16));
    }];
    
    UILabel *lblAlert = [UILabel new];
    lblAlert.text = @"首次进入产品管理请先上传营业执照。提交完成，审核通过后即可进入产品管理。";
    lblAlert.numberOfLines = 0;
    lblAlert.textColor = kColorWithHex(0x666666);
    lblAlert.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:lblAlert];
    [lblAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblAlertRed);
        make.leading.equalTo(lblAlertRed.mas_trailing).offset(4);
        make.trailing.equalTo(self.view.mas_trailing).offset(-17);
    }];
    
    UIButton *btnCommit = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCommit setTitle:@"提交" forState:UIControlStateNormal];
    [btnCommit setTitleColor:kColorWithHex(0xffffff) forState:UIControlStateNormal];
    [btnCommit setBackgroundColor:kColorWithHex(0x70be68)];
    btnCommit.titleLabel.font = [UIFont systemFontOfSize:16];
    btnCommit.layer.cornerRadius = 4.0;
    btnCommit.layer.masksToBounds = YES;
    [btnCommit addTarget:self action:@selector(btnCommitClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCommit];
    [btnCommit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(lblAlert.mas_bottom).offset(132);
        make.size.mas_equalTo(CGSizeMake(319, 48));
    }];
}
#pragma mark - 交互方法
//图片选择
- (void)btnImageClickAction:(UIButton *)sender{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.allowsEditing = YES;
    picker.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:picker controller:self sourceView:nil];
}
//删除
- (void)btnDeleteClickAction:(UIButton *)sender{
    [self.btnImage setImage:[UIImage imageNamed:@"user_bussiness_license"] forState:UIControlStateNormal];
    self.ivImage = nil;
    self.btnDelete.hidden = YES;
}
//提交
- (void)btnCommitClickAction:(UIButton *)sender{
    if (self.ivImage) {
        [MBProgressHUD showHUDAddedTo:self.view msg:@"上传营业执照..." animated:YES];
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [NetWorkTool UploadPicWithUrl:uploadAgentLicense param:param key:@"images" image:self.ivImage withSuccess:^(id dic) { 
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            BBussinessLicenseUploadView *viewNotice = [[BBussinessLicenseUploadView alloc] init];
            viewNotice.delegate = self;
            [viewNotice showView];
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        
    }else{
        ShowToastWithText(@"请选择您的营业执照");
    }
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    //图片类型
    UIImage *theImage = nil;
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        // 判断，图片是否允许修改
        if ([picker allowsEditing]){
            // 获取用户编辑之后的图像
            theImage = [info objectForKey:UIImagePickerControllerEditedImage];
        }else {
            // 获取原始的照片
            theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        [self.btnImage setImage:theImage forState:UIControlStateNormal];
        self.ivImage = theImage;
        self.btnDelete.hidden = NO;
    }
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - BBussinessLicenseUploadViewDelegate
- (void)viewBtnOKClick{
    //返回到根界面
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 懒加载
- (UIButton *)btnImage{
    if (!_btnImage) {
        _btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnImage.frame = CGRectMake(16, 55, 100, 100);
        [_btnImage setImage:[UIImage imageNamed:@"user_bussiness_license"] forState:UIControlStateNormal];
        [_btnImage addTarget:self action:@selector(btnImageClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnImage;
}
- (UIButton *)btnDelete{
    if (!_btnDelete) {
        _btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDelete setImage:[UIImage imageNamed:@"deldeImage"] forState:UIControlStateNormal];
        [_btnDelete addTarget:self action:@selector(btnDeleteClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnDelete.hidden = YES;
    }
    return _btnDelete;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
